package vn.allmobile.happyvoucher.activity;

import vn.allmobile.happyvoucher.R;
import vn.allmobile.happyvoucher.fragment.MenuLeftFragment;
import vn.allmobile.happyvoucher.fragment.MenuLeftSettingFragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.LayoutParams;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.OnOpenedListener;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

public class BaseMenuActivity extends BaseActivity {
	
	TextView navTitle ;
	ImageView navSearch;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		 
		if (savedInstanceState == null) {
			 
		} else {
			 
		}
		
		initActionbar(); 
		initSlideMenu();
	}
	
	
	public void initActionbar(){
		
		ActionBar actionBar = getSupportActionBar();
		actionBar.setCustomView(getLayoutInflater().inflate(R.layout.navigation_bar,null), new ActionBar.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
	}
	
	
	public void initUI(){
		if(getSupportActionBar() != null && getSupportActionBar().getCustomView() != null){
			this.navTitle = (TextView)getSupportActionBar().getCustomView().findViewById(R.id.nav_title);
			if(this.navTitle != null && getTitle() != null){
				this.navTitle.setText(getTitle());
			}

		}
	}
	
	public void initSlideMenu(){
		SlidingMenu menu = getSlidingMenu();
		setBehindContentView(R.layout.sliding_menu_left_frame);
		menu.setSecondaryMenu(R.layout.sliding_menu_right_frame);
		FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
		transaction.replace(R.id.menu_left_framelayout, new MenuLeftFragment());
		transaction.commit();
		menu.setBehindOffset(convertDipToPixels(50.0F));
		menu.setMode(SlidingMenu.LEFT_RIGHT);
		menu.setOnOpenedListener(new OnOpenedListener() {
			
			@Override
			public void onOpened() {
				BaseMenuActivity.this.forceHideSoftKeyboard();
				
			}
		});
		
		menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
		
		
	}
	
	public int convertDipToPixels(float size)
	{
	   return (int)(0.5F + size * getResources().getDisplayMetrics().density);
	}
	
	public void onClick_LeftSlideMenu(View v){
		showMenu();
	}
	
	public void onClick_RighSlideMenu(View v){
		showSecondaryMenu();
	}
	
	public void onClick_GoHomePage(View paramView)
	{
	    Intent intent = new Intent(this, MainActivity.class);
	    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);	    
	    startActivity(intent);
	}
	
	public void onClick_BackFromMenuLeftSettings(View paramView)
	{
	  FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
	  transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
	  transaction.replace(R.id.menu_left_framelayout, Fragment.instantiate(this, new MenuLeftFragment().getClass().getName())).commit();
	}
	
	public void onClick_NearMe(View v)
	{
	    
	}
	
	public void onClick_Settings(View paramView)
    {
	    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
	    transaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left);
	    transaction.replace(R.id.menu_left_framelayout, Fragment.instantiate(this, new MenuLeftSettingFragment().getClass().getName())).commit();
    }
	
	
	private void forceHideSoftKeyboard(){
		try{
			InputMethodManager input = (InputMethodManager)getSystemService( Context.INPUT_METHOD_SERVICE);
		      if ((getCurrentFocus() != null) && (input != null))
		        input.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 2);
		}catch(Exception e){
			
		}
	}
}
